<template>
	<view class="wrap">
		<view class="u-avatar-wrap">
			<image class="u-avatar-demo" :src="avatar" mode="aspectFill"></image>
		</view>
		<u-button @tap="chooseAvatar">选择图片</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatar: uni.getStorageSync('avatar') || 'https://cdn.uviewui.com/uview/common/logo.png',
			}
		},
		 
		 
		created() {
			// 监听从裁剪页发布的事件，获得裁剪结果
			uni.$on('uAvatarCropper', path => {
				this.avatar = path;
				// 可以在此上传到服务端
				console.log("path: ",path);
				uni.uploadFile({
					url: 'http://www.example.com/upload',
					filePath: path,
					name: 'file',
					complete: (res) => {
						console.log(res);
					}
				});
			})
		},
		methods: {
			 chooseAvatar(){
			 	uni.navigateTo({url:'/uview-ui/components/u-avatar-cropper/u-avatar-cropper'}) 
			 }
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		padding: 40rpx;
	}

	.u-avatar-wrap {
		margin-top: 80rpx;
		overflow: hidden;
		margin-bottom: 80rpx;
		text-align: center;
	}

	.u-avatar-demo {
		width: 150rpx;
		height: 150rpx;
		border-radius: 100rpx;
	}
</style>